package sanshi.contect.udpstarter.runner;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;


import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import sanshi.contect.udpstarter.entity.UDPEntity;
import sanshi.contect.udpstarter.handler.ConsumerMsgHandler;
import sanshi.contect.udpstarter.server.UDPServer;

import java.util.concurrent.CompletableFuture;


/**
 * udp server端启动
 */
@Slf4j
@Order(value = 1)
@Configuration
public class UDPServerStartApplicationRunner implements ApplicationRunner {


    @Autowired
    private UDPServer udpServer;

    @Autowired
    private UDPEntity udpEntity;

    @Autowired
    private ConsumerMsgHandler consumerMsgHandler;

    @Override
    public void run(ApplicationArguments args) {

        if (udpEntity.isStartServer()) {

            CompletableFuture.runAsync(() -> {
                try {
                    udpServer.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            log.info("------------------udp服务端启动成功------------");

            CompletableFuture.runAsync(() -> {
                consumerMsgHandler.getMsgFromQueue();
            });
            log.info("------------------数据存储队列监听成功------------");

        }
    }
}